# coding:utf-8
# @Time     : 2018/11/13 2:58 PM
# @Author   : 程杨 
# @Discribe : 启动模型，传入用户imei/applytime，返回applist/input输入法风控得分
if __name__ == '__main__':
    import jieba
    import time
    from model.applist_ContactExtractor import Applist_ContactExtractor
    from model.input_ContactExtractor import Input_ContactExtractor
    from sklearn.externals import joblib
    applist_model = joblib.load('/Users/apple/Desktop/itfan/model/applist_lgb_20181119_model.m')
    tfidf_model = joblib.load('/Users/apple/Desktop/itfan/model/input_tfidf_20181104_model.m')
    input_model = joblib.load('/Users/apple/Desktop/itfan/model/input_lgb_20181104_model.m')
    from collections import OrderedDict
    jieba.initialize()
    from common.logger import Logger
    # 测试数据
    imei = '123123123123'
    apply_time = '20181111'
    apply_id = 0
    applist_data = [{
        'date': '20181101',
        'androidid': '484257ef5eaee3d8',
        'country': 'CN',
        'province': '44',
        'city': '4420',
        'version': '8.15.22.199596',
        'device': 'PBAT00',
        'brand': 'oppo',
        'applist': [{
            'package_name': 'com.zhongfox.luoyang',
            'is_sys': 1,
            'time': 4550,
            'usage_num': 100,
            'timestamps': ''
        }, {
            'package_name': 'com.tencent.mm',
            'is_sys': 0,
            'time': 4550,
            'usage_num': 100,
            'timestamps': ''
        }]
    }, {
        'date': '20181102',
        'androidid': '484257ef5eaee3d8',
        'country': 'CN',
        'province': '44',
        'city': '4420',
        'version': '8.15.22.199596',
        'device': 'PBAT00',
        'brand': 'oppo',
        'applist': [{
            'package_name': 'com.zhongfox.luoyang',
            'is_sys': 1,
            'time': 4550,
            'usage_num': 100,
            'timestamps': ''
        }, {
            'package_name': 'com.tencent.mm',
            'is_sys': 0,
            'time': 4550,
            'usage_num': 100,
            'timestamps': ''
        }]
    }]
    applist_data = []

    input_data = [{'prefix': 'sc',
                   'input_time': '20181022',
                   'content': '阿斯达大口啖十多年安苏到静安寺点击阿斯达加时间段卡视角的阿萨德级武器级武器难道你能上去你今年的拿伞按时',
                   'package_name': 'asasd'
                   },
                  {'prefix': 'sc',
                   'input_time': '20181022',
                   'content': '阿斯顿撒多贷款王阿斯顿撒多贷款王阿斯顿撒多贷款王阿斯顿撒多贷款王阿斯达大口啖十多年安苏到静安寺点击阿斯达加时间段卡视角的阿萨德级武器级武器难道你能上去你今年的拿伞按时',
                   'package_name': 'asasd'
                   }]
    input_data = []
    app_ex = Applist_ContactExtractor()
    input_ex = Input_ContactExtractor()
    logger = Logger("score.log").getLogger()
    res = OrderedDict()
    t1 = time.time()
    res['imei'] = imei
    res['apply_time'] = apply_time
    res['apply_id'] = apply_id
    app_res = app_ex.parse(apply_time, applist_data, applist_model)
    input_res = input_ex.parse(imei, input_data, tfidf_model, input_model)
    res.update(app_res)
    res.update(input_res)
    logger.info('{},{}'.format(res, time.time() - t1))
